﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using BaseHelper.Http;
using BaseHelper;
using BaseHelper.Base;


namespace DeviceSDK.NetTraffic.Gaode
{
    public class GaodeMap_JiaoTong
    {
        public static TravelInfo Get_Travel_Time(string StartPos, string PassPos, string EndPos)
        {
            try
            {
                HttpHelperLite http = new HttpHelperLite();

                string[] sn = StartPos.Split(',');
                string[] en = EndPos.Split(',');
                string[] ps = PassPos.Length==0?en:PassPos.Split(',');

                double slat, slng, plat, plng, elat, elng;

                MapConverter.BD09ToGCJ02(sn[0], sn[1], out slat, out slng);
                MapConverter.BD09ToGCJ02(ps[0], ps[1], out plat, out plng);
                MapConverter.BD09ToGCJ02(en[0], en[1], out elat, out elng);


                sn[0] = slat.ToString();
                sn[1] = slng.ToString();
                ps[0] = plat.ToString();
                ps[1] = plng.ToString();
                en[0] = elat.ToString();
                en[1] = elng.ToString();


                StartPos = sn[1] + "," + sn[0];
                PassPos = ps[1] + "," + ps[0];
                EndPos = en[1] + "," + en[0];

                string Url = "https://restapi.amap.com/v3/direction/driving?origin=" + StartPos + "&destination=" + EndPos + "&waypoints=" + PassPos + "&extensions=all&output=json&key=" + NetTrafficHelper.Api_Key_Gaode;//9d357930dcc0c8184e5fa5ec4994b182";

                try
                {
                    string Html = http.Get(Url);
                    if (Html.Contains("status"))
                    {
                        Hashtable htRet = (Hashtable)JsonHelper.Decode(Html);
                        if (htRet["status"].ToString() != "1")
                        {
                           return null;
                        }
                        if (htRet["info"].ToString() != "OK")
                            return null;

                        Hashtable htRoutes = (Hashtable)htRet["route"];
                        ArrayList htPaths = (ArrayList)htRoutes["paths"];
                        Hashtable htFirstPaths = (Hashtable)htPaths[0];
                        ArrayList htSetp = (ArrayList)htFirstPaths["steps"];

                        int total_distance = Convert.ToInt32(htFirstPaths["distance"]);
                        int total_duration = Convert.ToInt32(htFirstPaths["duration"]);

                        float base_distance = 0;
                        //float real_distance = 0;

                        int max_jam = -1;
                        int first_jam = -1;
                        float first_distance = -1;
                        float max_distance = -1;

                        List<SectionInfo> lt = new List<SectionInfo>();
                        string steps = "";

                        //for (int i = 0; i < htSetp.Count; i++)
                        //{
                        //    Hashtable ht = (Hashtable)htSetp[i];
                        //    real_distance += Convert.ToSingle(ht["distance"].ToString());
                        //}

                        for (int i = 0; i < htSetp.Count; i++)
                        {
                            Hashtable ht = (Hashtable)htSetp[i];
                            ArrayList alConditions = (ArrayList)ht["tmcs"];

                            steps += ht["polyline"].ToString();

                            if (i < (htSetp.Count - 1))
                                steps += ";";

                            foreach (Hashtable condition in alConditions)
                            {
                                int step_jam = 0;

                                if (condition["status"].ToString() == "严重拥堵")
                                    step_jam = 4;
                                else if (condition["status"].ToString() == "拥堵")
                                    step_jam = 3;
                                else if (condition["status"].ToString() == "缓行")
                                    step_jam = 2;
                                else if (condition["status"].ToString() == "畅通")
                                    step_jam = 1;
                                else
                                    step_jam = 0;

                                float step_distance = Convert.ToSingle(condition["distance"].ToString());


                                if (step_jam == 4)
                                    step_jam = 3;

                                if (step_jam > max_jam)
                                {
                                    max_jam = step_jam;
                                    max_distance = Convert.ToInt32(base_distance);
                                }

                                if (first_jam < 3)
                                {
                                    if (first_jam != 3 && step_jam == 3)
                                    {
                                        first_jam = 3;
                                        first_distance = Convert.ToInt32(base_distance);
                                    }
                                    else if (first_jam < 2 && step_jam == 2)
                                    {
                                        first_jam = 2;
                                        first_distance = Convert.ToInt32(base_distance);
                                    }
                                }

                                float percent = step_distance / total_distance;

                                lt.Add(new SectionInfo(Convert.ToInt32(base_distance),Convert.ToInt32(step_distance), step_jam, percent));
                                base_distance += step_distance;


                            }
                        }

                        return new TravelInfo(total_distance, total_duration, first_jam, first_distance, max_jam, max_distance, steps, lt.ToArray());
                    }

                }
                catch { }

            }
            catch { }

            return null;
        }
    }
}
